package com.pjchat.pjchatclient.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JOptionPane;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ita.im.model.ErrorMessageType;
import com.ita.im.model.Message;
import com.ita.im.model.Unit;
import com.ita.im.model.User;

public class JsonUtil {

	@SuppressWarnings("unchecked")
	public static Unit jsonObjectToUnit(String object) {
		Unit unit = null;

		ObjectMapper mapper = new ObjectMapper();
		try {
			try {
				unit = mapper.readValue(object, Unit.class);
			} catch (NullPointerException e) {
				int a = JOptionPane.NO_OPTION;
				while (a != 0) {
					a = JOptionPane.showConfirmDialog(null,
							"Can't connect to server,please try later", "温馨提示",
							JOptionPane.YES_NO_OPTION);
					switch (a) {
					case JOptionPane.YES_OPTION:
						System.exit(0);
						break;
					default:
						break;
					}
				}
				e.printStackTrace();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		Unit result = new Unit();
		Map<String, Object> map = new HashMap<String, Object>();
		if (unit.getValue().containsKey("user")) {
			LinkedHashMap<String, Object> user = (LinkedHashMap<String, Object>) unit
					.getValue().get("user");
			map.put("user", beUser(user));
		}
		if (unit.getValue().containsKey("message")) {
			LinkedHashMap<String, Object> message = (LinkedHashMap<String, Object>) unit
					.getValue().get("message");
			Message message2 = new Message();
			message2.setFrom(beUser((LinkedHashMap<String, Object>) message
					.get("from")));
			message2.setTo(beUser((LinkedHashMap<String, Object>) message
					.get("to")));
			message2.setContent((String) message.get("content"));
			message2.setShake((boolean) message.get("shake"));
			message2.setTime(new Date((long) message.get("time")));
			message2.setToAll((boolean) message.get("toAll"));
			map.put("message", message2);
		}
		if (unit.getValue().containsKey("users")) {
			List<LinkedHashMap<String, Object>> users = (List<LinkedHashMap<String, Object>>) unit
					.getValue().get("users");
			List<User> users2 = new ArrayList<User>();
			for (int i = 0; i < users.size(); i++) {
				users2.add(beUser(users.get(i)));
			}
			map.put("users", users2);
		}
		if (unit.getValue().containsKey("error_message")) {
			// map.put("error_message",(ErrorMessageType)
			// unit.getValue().get("error_message"));
			map.put("error_message",
					ErrorMessageType.valueOf((String) unit.getValue().get(
							"error_message")));

		}
		result.setType(unit.getType());
		result.setValue(map);
		result.setSuccess(unit.isSuccess());
		return result;
	}

	private static User beUser(LinkedHashMap<String, Object> map) {
		User user = new User();
		user.setName((String) map.get("name"));
		user.setPassword((String) map.get("password"));
		return user;
	}

	public static String unitToJsonObject(Unit unit) {
		ObjectMapper mapper = new ObjectMapper();
		String string = null;
		try {
			string = mapper.writeValueAsString(unit);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return string;
	}

}
